package com.goods.business.service.imp;

import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    //分页展示入库数据
    @Override
    public Map showSupplier(Map pageMap) {
        List<Supplier> supplierList;
        String address = (String) pageMap.get("address");
        String contact = (String) pageMap.get("contact");
        String name = (String) pageMap.get("name");
        //省市区县&&联系人&&具体地点
        if (name==null && address==null && contact==null) {
            supplierList = supplierMapper.selectAll();
        } else {
            supplierList = supplierMapper.selectByNameAddressContact(name,address,contact);
        }

        List<SupplierVO> supplierVOList = SupplierConverter.converterToSupplierVOList(supplierList);
        String pageSize = pageMap.get("pageSize").toString();
        int pageSizeInt = Integer.parseInt(pageSize);
        String pageNum = pageMap.get("pageNum").toString();
        int pageNumInt = Integer.parseInt(pageNum);
        List<SupplierVO> page = ListPageUtils.page(supplierVOList, pageSizeInt, pageNumInt);
        Map result = new HashMap();
        result.put("rows",page);
        result.put("total",supplierVOList.size());
        return result;
    }

    //添加
    @Override
    public void addSupplier(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }

    //编辑弹窗回显
    @Override
    public SupplierVO showEdit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = SupplierConverter.converterToSupplierVO(supplier);
        return supplierVO;
    }

    //编辑
    @Override
    public void updateSupplier(Long id, SupplierVO supplierVO) {
        supplierVO.setId(id);
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplierMapper.updateByPrimaryKey(supplier);
    }

    //删除
    @Override
    public void deleteSupplier(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Supplier> findAll() {
        return supplierMapper.selectAll();
    }
}
